package com.homelink.android.explorerup.activity;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.homelink.android.explorerup.R;

public class NewFilesActivity extends Activity implements OnClickListener {
	public static final int REQUEST_CODE_NEW_FOLDER = 1;
	public static final int REQUEST_CODE_NEW_FILE = 2;
	public static final String CURRENT_PATH = "CURRENT_PATH";
	public static final String REQUEST_TYPE = "REQUEST_TYPE";

	public static final String TAG = "NewFilesActivity";

	private Button mBtnCancel;
	private Button mBtnConfirmCreateFile;
	private Button mBtnConfirmCreateFolder;
	private EditText mEditNewFileName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.newfiles);
		// int requestCode = getIntent().getIntExtra(REQUEST_TYPE,
		// REQUEST_CODE_NEW_FOLDER);
		// if(requestCode == REQUEST_CODE_NEW_FILE){
		setTitle(R.string.title_new_file);
		// }else{
		// setTitle(R.string.title_new_dir);
		// }

		mBtnCancel = (Button) findViewById(R.id.btnCancel);
		mBtnConfirmCreateFile = (Button) findViewById(R.id.btnConfirmCreateFile);
		mEditNewFileName = (EditText) findViewById(R.id.editTextNewFileName);
		mBtnCancel.setOnClickListener(this);
		mBtnConfirmCreateFile.setOnClickListener(this);

		mBtnConfirmCreateFolder = (Button) findViewById(R.id.btnConfirmCreateFolder);
		mBtnConfirmCreateFolder.setOnClickListener(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			this.finish();
			this.overridePendingTransition(R.anim.push_bottom_in,
					R.anim.push_bottom_out);
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	public void onClick(View v) {
		if (v == mBtnCancel) {
			this.finish();
			this.overridePendingTransition(R.anim.push_bottom_in,
					R.anim.push_bottom_out);
		} else if (v == mBtnConfirmCreateFile || v == mBtnConfirmCreateFolder) {
			Editable txt = mEditNewFileName.getText();
			if (txt != null && !txt.toString().trim().equals("")) {
				String currentPath = this.getIntent().getStringExtra(
						CURRENT_PATH);
				
				Log.d(TAG, "currentPath:" + currentPath);

				currentPath = currentPath + File.separator + txt.toString();
				File f = new File(currentPath);

				if (v == mBtnConfirmCreateFile) {
					try {
						if (f.createNewFile()) {
							// Toast.makeText(NewFilesActivity.this,
							// R.string.msg_create_file_error, 1000).show();
						}
					} catch (IOException e) {
						Toast.makeText(NewFilesActivity.this,
								R.string.msg_create_file_error, 1000).show();
						e.printStackTrace();
					}
				} else if (f.mkdir()) {
					Toast.makeText(NewFilesActivity.this,
							R.string.msg_create_file_error, 1000).show();
				}
				this.finish();
				this.overridePendingTransition(R.anim.push_bottom_in,
						R.anim.push_bottom_out);
			} else {

				Animation shake = AnimationUtils.loadAnimation(this,
						R.anim.shake);
				mEditNewFileName.startAnimation(shake);
			}

		}

	}

}
